package com.gosophia.govDoc.action.turning;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.commons.web.logAnnotation.LogRecord;
import com.gosophia.govDoc.common.TurningAction;
import com.gosophia.govDoc.dto.DocumentJobDataDTO;
import com.gosophia.govDoc.ws.DocumentTurningWebService;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller("SendBackAction")
public class SendBackAction extends BaseSupportAction{//用于审批页面【退回】按钮

	private Long jobId;
	
	private final Logger logger = Logger.getLogger(this.getClass());
	
    private DocumentJobDataDTO detailData;
    
    private String fromFlag;
    
	@Autowired
	private DocumentTurningWebService documentTurningWebService;
	
	@LogRecord(eventAction = "文档置办-退回", eventTarget = "detailData")
	@Override
	public String _execute() throws Exception {
	    logger.debug("进入退回sendBackAction");
	    
		//取到当前的操作用户
  		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
		.getContext().getAuthentication().getPrincipal();
  		detailData.setOperatorDTO(userDetails.getUserAccountDTO().getUserInfoDTO());
		if(detailData.getDocumentId()==null){
		    logger.error("应当传回文档id");
		}
		logger.debug("文档退回操作后的文档版本：=>"+ detailData.getDocumentVersion());
  		documentTurningWebService.handleDocumentTurning(jobId, detailData, TurningAction.SENDBACK);
		super.getRequestResult().setRsltCode("0");
		super.getRequestResult().setMessage("文档退回成功");
		ActionContext.getContext().getSession().put("_lastOpResult", super.getRequestResult());
		if("task".equals(fromFlag)){
		    return "TASK";
		}else{
	        return SUCCESS;  
		}
	}

	public void setDetailData(DocumentJobDataDTO detailData) {
		this.detailData = detailData;
	}

	public DocumentJobDataDTO getDetailData() {
		return detailData;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public Long getJobId() {
		return jobId;
	}

    public String getFromFlag() {
        return fromFlag;
    }

    public void setFromFlag(String fromFlag) {
        this.fromFlag = fromFlag;
    }
	
}
